package org.edu.modules.jxzy.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxzy.api.CommonApi;
import org.edu.modules.jxzy.api.TestSystemApi;
import org.edu.modules.jxzy.entity.JwJiaoshi;
import org.edu.modules.jxzy.service.IJwJianzhuService;
import org.edu.modules.jxzy.service.IJwJiaoshiService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

 /**
 * @Description: 教室信息表
 * @Author: jeecg-boot
 * @Date:   2021-04-15
 * @Version: V1.0
 */
@Api(tags="教室信息表")
@RestController
@RequestMapping("/jxzy/jwJiaoshi")
@Slf4j
public class JwJiaoshiController extends BaseController<JwJiaoshi, IJwJiaoshiService> {
	@Autowired
	private IJwJiaoshiService jwJiaoshiService;
	@Autowired
	private IJwJianzhuService jwJianzhuService;
	@Autowired
	private CommonApi commonApi;
	@Autowired
	private TestSystemApi systemApi;
	
	/**
	 * 分页列表查询
	 *
	 * @param jwJiaoshi
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "教室信息表-分页列表查询")
	@ApiOperation(value="教室信息表-分页列表查询", notes="教室信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(JwJiaoshi jwJiaoshi,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JwJiaoshi> queryWrapper = QueryGenerator.initQueryWrapper(jwJiaoshi, req.getParameterMap());
		Page<JwJiaoshi> page = new Page<JwJiaoshi>(pageNo, pageSize);
		IPage<JwJiaoshi> pageList = jwJiaoshiService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param jwJiaoshi
	 * @return
	 */
	@AutoLog(value = "教室信息表-添加")
	@ApiOperation(value="教室信息表-添加", notes="教室信息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody JwJiaoshi jwJiaoshi) {
		jwJiaoshiService.save(jwJiaoshi);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jwJiaoshi
	 * @return
	 */
	@AutoLog(value = "教室信息表-编辑")
	@ApiOperation(value="教室信息表-编辑", notes="教室信息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody JwJiaoshi jwJiaoshi) {
		jwJiaoshiService.updateById(jwJiaoshi);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教室信息表-通过id删除")
	@ApiOperation(value="教室信息表-通过id删除", notes="教室信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		jwJiaoshiService.removeById(id);
		return Result.ok("删除成功!");
	}

	 /**
	  *   通过id获取信息
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "教材信息表-通过id删除")
	 @ApiOperation(value="教材信息表-通过id删除", notes="教材信息表-通过id删除")
	 @GetMapping(value = "/getById")
	 public Result<?> getById(@RequestParam(name="id",required=true) int id) {
		 JwJiaoshi jiaoshi = jwJiaoshiService.getById(id);
		 if(jiaoshi==null){
		 	return Result.error("查无此教室信息！");
		 }
		 return Result.ok(jiaoshi);
	 }


	 @AutoLog(value = "FeignClient-教务教室信息-通过名称获取教室信息")
	 @GetMapping(value = "/getJsListByName")
	 List<JwJiaoshi> getJsListByName(@RequestParam(name="name",required=true) String name){
		 LambdaQueryWrapper<JwJiaoshi> queryWrapper=new LambdaQueryWrapper<>();
		 queryWrapper.eq(JwJiaoshi::getJsmc,name);
		 return jwJiaoshiService.list(queryWrapper);
	 }
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "教室信息表-批量删除")
	@ApiOperation(value="教室信息表-批量删除", notes="教室信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jwJiaoshiService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "教室信息表-通过id查询")
//	@ApiOperation(value="教室信息表-通过id查询", notes="教室信息表-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		JwJiaoshi jwJiaoshi = jwJiaoshiService.getById(id);
//		if(jwJiaoshi==null) {
//			return Result.error("未找到对应数据");
//		}
//		return Result.ok(jwJiaoshi);
//	}




    /**
    * 导出excel
    *
    * @param request
    * @param jwJiaoshi
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JwJiaoshi jwJiaoshi) {
        return super.exportXls(request, jwJiaoshi, JwJiaoshi.class, "教室信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JwJiaoshi.class);
    }


	 /**
	  * 批量启用/禁用
	  *
	  * @param ids
	  * @param status
	  * @return
	  */
	 @AutoLog(value = "教室信息表-批量启用/禁用")
	 @ApiOperation(value="教室信息表-批量启用/禁用", notes="教室信息表-批量启用/禁用")
	 @GetMapping(value = "/updateStatusBatch")
	 public Result<?> updateStatusBatch(@RequestParam(name="ids",required=true) String ids,
										@RequestParam(name="status",required = true)int status) {
		 jwJiaoshiService.updateStatusByIds(ids,status);
		 return Result.ok("更新状态成功!");
	 }

	 /**
	  * 校园教室数量统计
	  * @return
	  */
	 @AutoLog(value = "教室信息表-校园教室数量统计")
	 @ApiOperation(value="教室信息表-校园教室数量统计", notes="教室信息表-校园教室数量统计")
	 @GetMapping("/statistics")
	 public Result<?> statistics(HttpServletRequest request){

	 	 String[] names = {"", "多媒体教室", "语音室", "实验室", "计算机房", "普通教室", "专用教室", "实训室"};

		 // 获取所有校区信息
		 List<JSONObject> result = commonApi.queryCampusList(CommonApi.COMMON_TOKEN_KEY).getResult();

		 for(JSONObject item : result){
		 	 // 教学楼数量
			 item.put("jxlsl", jwJianzhuService.queryCountByXqId(item.getInteger("id")));
			 // 总教室数量
			 item.put("zjssl", jwJiaoshiService.queryCountByXqId(item.getInteger("id")));
			 JSONArray jslxsl = new JSONArray();
			 // todo 暂时写死
			 for(int i = 1; i <= 7; i++){
			 	 JSONObject temp = new JSONObject();
			 	 temp.put("id", i);
			 	 temp.put("name", names[i]);
			 	 temp.put("count", jwJiaoshiService.queryCountByJslx(item.getInteger("id"), String.valueOf(i)));
				 jslxsl.add(temp);
			 }
			 // 教室类型数量
			 item.put("jslxsl", jslxsl);
		 }

	 	 return Result.OK(result);
	 }

	 /**
	  * 获取教室完整名称（建筑物-教室名称）
	  * @param id 教室id
	  * @return 名称
	  */
	 @RequestMapping("/queryNameById")
	 public String queryNameById(Integer id){
	 	return jwJiaoshiService.queryNameById(id);
	 }

}
